package com.example.juc;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
 * @Description
 * @Date 2021/7/24
 * @Created by Jonathan
 */
@Slf4j
public class UpdateField {
    public static void main(String[] args) {
        Student student = new Student();
        AtomicReferenceFieldUpdater<Student, String> fieldUpdater =
                AtomicReferenceFieldUpdater.newUpdater(Student.class, String.class, "name");
        fieldUpdater.compareAndSet(student, null, "张三");
        log.info("s:{}", student);
    }
}

class Student {
    /*CAS配合volatile保证可见性*/
    volatile String name;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }
}
